package Jkdk;

import net.dongliu.requests.Requests;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author yyq
 * @create 2021-09-02 19:43
 */
public class TestMain{
    //登录时请求的Url
    static final String url_dl="https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/login";
    static final String url_zy="https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb?";
    static final String url_bd="https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb";
    static final String url_tj="https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb";
    //登录时请求的Header
    static Map header_dl=new HashMap();
    static Map bd=new HashMap();

    @Test
    public void test() throws DocumentException {
        List<User> users = getUsers();
        System.out.println(users);
    }

    static {
        //登录时请求的Header设置
        header_dl.put("Content-Type","application/x-www-form-urlencoded");
        header_dl.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 HBPC/11.0.6.301");
        header_dl.put("Referer","https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0");
        bd.put("myvs_1","否");
        bd.put("myvs_2","否");
        bd.put("myvs_3","否");
        bd.put("myvs_4","否");
        bd.put("myvs_5","否");
        bd.put("myvs_6","否");
        bd.put("myvs_7","否");
        bd.put("myvs_8","否");
        bd.put("myvs_9","否");
        bd.put("myvs_10","否");
        bd.put("myvs_11","否");
        bd.put("myvs_12","否");
        bd.put("myvs_13","g");
        bd.put("myvs_13a","41");
        bd.put("myvs_13b","4116");
        bd.put("myvs_13c","河南省.周口市");
        bd.put("myvs_24","否");
        bd.put("memo22","成功获取");
        bd.put("did","2");
        bd.put("door","");
        bd.put("day6","b");
        bd.put("men6","a");
        bd.put("sheng6","");
        bd.put("shi6","");
        bd.put("fun3","");
        bd.put("jingdu","0");
        bd.put("weidu","0");
    }



    public static void main(String[] args) throws Exception {
        //获取用户信息
        List<User> userList = getUsers();
        for (User user : userList) {
            String zh=user.getZh();
            String pwd=user.getPassword();
            String email=user.getEmail();
            Map param_dl=new HashMap();
            param_dl.put("uid",zh);
            param_dl.put("upw", pwd);
            param_dl.put("smbtn","进入健康状况上报平台");
            param_dl.put("hh28","540");

            String readToText = Requests.post(url_dl).headers(header_dl).forms(param_dl)
                    .send().readToText();
            System.out.println(readToText);

            String url_dl_="";
            if(getUrl(readToText)!=null){
                url_dl_=getUrl(readToText);
            }else {
              throw new Exception("获取登录返回地址错误");
            }


            Map ptopidAndSid = getPidAndSid(url_dl_);
            Requests.get(url_dl_).send().readToText();
            String s = Requests.get(url_zy + "ptopid=" + ptopidAndSid.get("ptopid") + "&sid=" + ptopidAndSid.get("sid") + "&fun2=")
                    .send().readToText();



            Map hq_bd=new HashMap();
            hq_bd.putAll(ptopidAndSid);
            hq_bd.put("day6","b");
            hq_bd.put("did","1");
            hq_bd.put("door","");
            hq_bd.put("men6","a");
            Map header=new HashMap();
            header.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 HBPC/11.0.6.301");
            header.put("Content-Type","application/x-www-form-urlencoded");
            header.put("Referer",url_zy + "ptopid=" + ptopidAndSid.get("ptopid") + "&sid=" + ptopidAndSid.get("sid") + "&fun2=");
             Requests.post(url_bd).headers(header).
                                forms(bd).send().readToText();


            bd.putAll(ptopidAndSid);
            String s1 = Requests.post(url_tj).forms(bd).send().readToText();
            System.out.println(s1);
            SendEmail.send(user.getEmail(),s1);


        }


    }

    //获取用户信息
    public static List<User> getUsers() throws DocumentException {
        //创建一个SAXReader
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("User.xml");
        System.out.println(document);

        //1 读取user.xml对象获取根元素
        //2 通过根元素获取
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements("User");
        List<User> usersList=new ArrayList();
        for (Element user : elements) {
            String name = user.elementText("name");
            String zh = user.elementText("zh");
            String pwd = user.elementText("password");
            String email = user.elementText("email");
            usersList.add(new User(name,zh,pwd,email));
        }
        return usersList;
    }

    //提取Url
    static String getUrl(String text){
        Pattern pattern = Pattern.compile("https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
        Matcher matcher = pattern.matcher(text);

        String url="";
        if (matcher.find()) {
            url= matcher.group().toString();
            return url;
        }
        return null;
    }

    //解析出来ptopid和sid
    static Map getPidAndSid(String text){
        String[] split = text.split("pid=");
        String[] split1 = split[1].split("&sid=");
        for (String s : split1) {
            System.out.println(s);
        }
        Map map=new HashMap();
        map.put("ptopid",split1[0]);
        map.put("sid",split[1]);
        return map;
    }

}
